function praise(index){
    var num=$("#num_"+index).html();
    var fid=$("#fid_"+index).val();
    $.ajax({
        url:URL_PREFIX+"/feedback/praise/"+fid,
        type:"get",
        statusCode:{
            200:function (data) {
                if(data){
                    swal({
                        title: "已取消点赞。",
                        text: "1秒后自动关闭。",
                        timer: 1000,
                        showConfirmButton: false
                    });
                    $("#num_"+index).html(parseInt(num)-1);
                    $("#like_"+index).css('color','#ccc');
                    $("#span_"+index).html("");
                }else{
                    swal({
                        title: "点赞成功！",
                        text: "1秒后自动关闭。",
                        timer: 1000,
                        showConfirmButton: false
                    });
                    $("#num_"+index).html(parseInt(num)+1);
                    $("#like_"+index).css('color','#f00');
                    $("#span_"+index).html("(已赞)");
                }
            },403:function () {
                swal({
                    title: "未登录！",
                    text: "稍后跳转到登录界面",
                    timer: 2000,
                    type: 'warning',
                    showConfirmButton: false
                },function () {
                    gotoLogin();
                });
            }
        }
    });
}
function gotoLogin() {
    window.location="login.html";
}

function addFeedback() {
    var anonymous=$("input[type=checkbox]:checked").val();
    if(anonymous!=1){
        anonymous=0;
    }
    if($("textarea").val()==""){
        swal("提交失败...","填写内容不能为空！","error");
        return;
    }
    var data={
        "content":$("textarea").val(),
        "anonymous":anonymous
    };
    $.ajax({
        url:URL_PREFIX+"/feedback/add",
        type:"post",
        data:JSON.stringify(data),
        contentType:"application/json",
        statusCode:{
            200:function () {
                swal({
                    title: "感谢您的反馈！",
                    text: "2秒后自动关闭。",
                    timer: 2000,
                    showConfirmButton: false
                });
                queryAllFeedbacks();
                $("textarea").val("");
                $("#anony").attr("checked",null);
            },403:function () {
                window.location="login.html";
            }
        }
    })
}
function checkIfPraised(fid) {
    var isPraised=false;
    $.ajax({
        url:URL_PREFIX+"/feedback/checkIfPraised/"+fid,
        type:"get",
        async:false,
        statusCode:{
            200:function (data) {
                isPraised=data;
            }
        }
    });
    return isPraised;
}
function queryAllFeedbacks(){
    $.ajax({
        url:URL_PREFIX+"/feedback/queryAll",
        type:"get",
        dataType:"text json",
        statusCode:{
            200:function (data) {
                var str="";
                $(".center-bottom").empty();
                $(data).each(function (index,val) {
                    var date=new Date(val.sendTime);
                    var anonymous=val.anonymous;
                    var userNameStr='',likeStr='',spanstr='';
                    if(anonymous==1){
                        userNameStr="<span id='userName'>匿名</span>";
                    }else{
                        userNameStr="   <a class='a_user_name' href=''>"+val.user.userName+"</a>" ;
                    }
                    if(checkIfPraised(val.fId)){
                        likeStr="<span class='like1' title='取消点赞' id='like_"+(index+1)+"' onclick='praise("+(index+1)+")'>&#10084;</span>";
                        spanstr="<span class='haspraised' id='span_"+(index+1)+"'>(已赞)</span>";
                    }else{
                        likeStr="<span class='like' title='赞一个' id='like_"+(index+1)+"' onclick='praise("+(index+1)+")'>&#10084;</span>";
                        spanstr="<span class='haspraised' id='span_"+(index+1)+"'></span>";
                    }
                    str="<div class='center-bottom-div'>" +
                        "<input type='hidden' id='fid_"+(index+1)+"' value='"+val.fId+"'>" +
                        "<div class='center-bottom-left'>" +
                        userNameStr+
                        "   ：<span class='feedback_content' id='content'>"+val.content+"</span>" +
                        "</div>" +
                        "<div class='center-bottom-right1'>" +
                        likeStr +
                        "   <span class='num' title='当前有"+val.likes+"人点赞' id='num_"+(index+1)+"'>"+val.likes+"</span>" +
                        spanstr +
                        "</div>" +
                        "<div class='center-bottom-right2'>" +
                        "   <div class='center-bottom-right21'>" +
                        "       <span class='time'>"+(index+1)+"楼</span>" +
                        "   </div>" +
                        "   <div class='center-bottom-right22'>" +
                        "       <span class='time'>"+convertDate(val.sendTime)+"</span>" +
                        "   </div>" +
                        "</div>" +
                        "</div>"+str;
                });
                $(".center-bottom").append(str);
            },403:function () {
                window.location="login.html";
            }
        }
    });
}
$(function () {
    queryAllFeedbacks();
});